package com.cat.graphTheory;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/
 * @create 2025/10/7 21:32
 * @since JDK17
 */

public class Solution41 {
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length, l = 0, r = -1;
        for (int i = 0, max = nums[0]; i < n; i++) {
            if (max > nums[i]) {
                r = i;
            } else {
                max = nums[i];
            }
        }
        for (int i = n -1, min = nums[i]; i >= 0; i--) {
            if (min < nums[i]) {
                l = i;
            } else {
                min = nums[i];
            }
        }

        return r - l + 1;
    }
}
